package com.qdlc.p2p.manage.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.qdlc.p2p.biz.processor.event.AccountCashModelValueEvent;
import com.qdlc.p2p.common.core.queue.QueueHandlers;
import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.AccountCashService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.AccountCash;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.model.AccountCashModel;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * 提现
 * @author linya
 */
public class ManageAccountCashAction extends BaseAction<AccountCashModel> implements ModelDriven<AccountCashModel> {

	@Resource
	private AccountCashService accountCashService;

	/**
	 * 提现记录
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/cashManager")
	public String cashManager() throws Exception {
		return "cashManager";
	}

	/**
	 * 提现记录列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/accountCashList")
	public void accountCashList() throws Exception {
		Map<String, Object> data = new HashMap<String, Object>();
		PageDataList<AccountCashModel> accountCashList = accountCashService.accountCashList(model);
		data.put("total", accountCashList.getPage().getTotal());
		data.put("rows", accountCashList.getList());
		printJson(getStringOfJpaObj(data));
	}

	
	
	/**
	 * 客服提现初审列表页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/verifyAccountCashManager")
	public String verifyAccountCashManager() throws Exception {
		return "verifyAccountCashManager";
	}

	/**
	 * 客服提现审核记录列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/verifyAccountCashList")
	public void verifyAccountCashList() throws Exception {
		if(StringUtil.isBlank(paramString("feeBear"))){
			model.setFeeBear(99);
		}
		if(StringUtil.isBlank(paramString("status"))){
			model.setStatus(4);
		}
		Map<String, Object> data = new HashMap<String, Object>();
		PageDataList<AccountCashModel> accountCashList = accountCashService.accountCashList(model);
		data.put("total", accountCashList.getPage().getTotal());
		data.put("rows", accountCashList.getList());
		printJson(getStringOfJpaObj(data));
	}

	/**
	 * 客服提现审核页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/kfVerifyCashEditPage")
	public String kfVerifyCashEditPage() throws Exception {
		AccountCash accountCash = accountCashService.find(model.getId());
		saveToken("verifyCashToken");
		request.setAttribute("accountCash", accountCash);
		request.setAttribute("user", UserUtils.getUserById(accountCash.getUserId()));
		return "kfVerifyCashEditPage";
	}

	/**
	 * 客服审核操作
	 * 
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/kfVerifyCashEdit")
	public void kfVerifyCashEdit() throws Exception {
		AccountCash accountCash = accountCashService.find(model.getId());
		model.validKFVerify(accountCash);
		checkToken("verifyCashToken");
		Operator operator = getOperator();
		operator.setRemark(model.getVerifyRemark());
		accountCashService.kfVerifyCash(accountCash, model.getStatus(), operator);
		
		printResult("操作成功！", true);
	}

	/**
	 * 财务提现审核列表页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/fullAccountCashManager")
	public String fullAccountCashManager() throws Exception {
		return "fullAccountCashManager";
	}

	/**
	 * 财务提现审核记录列表
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/fullAccountCashList")
	public void fullAccountCashList() throws Exception {
		Map<String, Object> data = new HashMap<String, Object>();
		if(model.getStatus()==0){
			model.setStatus(5); //客服初审通过
		}
		PageDataList<AccountCashModel> accountCashList = accountCashService.accountCashList(model);
		data.put("total", accountCashList.getPage().getTotal());
		data.put("rows", accountCashList.getList());
		printJson(getStringOfJpaObj(data));
	}

	/**
	 * 财务提现审核页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/cwFullCashEditPage")
	public String cwFullCashEditPage() throws Exception {
		AccountCash accountCash = accountCashService.find(model.getId());
		saveToken("fullVerifyCashToken");
		request.setAttribute("accountCash", accountCash);
		request.setAttribute("user", UserUtils.getUserById(accountCash.getUserId()));
		return "cwFullCashEditPage";
	}

	/**
	 * 财务提现审核操作
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/cwFullCashEdit")
	public void cwFullCashEdit() throws Exception {
		AccountCash accountCash = accountCashService.find(model.getId());
		model.validCWVerify(accountCash);
		checkToken("fullVerifyCashToken");
		accountCash.setStatus(model.getStatus());
		accountCash.setFee(model.getFee());
		accountCash.setAmount(BigDecimalUtil.sub(accountCash.getMoney(), model.getFee()));
		Operator operator = getOperator();
		operator.setRemark(model.getVerifyRemark());
		accountCashService.cwVerifyCash(accountCash, operator);
		printResult("操作成功！", true);
	}
	
	/**
	 * 导出报表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/exportExcelAccountCash")
	public void exportExcelAccountCash() throws Exception {
		response.setContentType("application/msexcel;charset=UTF-8");
		if(StringUtil.isBlank(paramString("feeBear"))){
			model.setFeeBear(99);
		}
		// 第一页开始
		model.setPage(1);
		// 最多出5000条记录
		model.setRows(5000);
		// 记录取得
		List<AccountCashModel> list = accountCashService.accountCashList(model).getList();

		String title = "提现管理Excel表_"+DateUtil.dateStr3(DateUtil.getNow());
		String[] hearders = new String[] { "编号", "用户名", "真实姓名", "状态", "银行卡", "所属银行", "提现总额(元)", "到账金额(元)", "手续费(元)", "手续费承担方", "提现时间 ", "状态 " };// 表头数组
		String[] fields = new String[] { "id", "userName", "realName", "statusStr", "bankNo", "bank", "moneyString", "amountString", "fee", "feeBearName", "addTime", "statusStr" };// 对象属性数组
		TableData td = ExcelUtils.createTableData(list, ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, getOperator().getUserName(), td);
	}
	/**
	 * 取消提现
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/cancelCash")
	public void cancelCash() throws Exception {
		AccountCash cash = accountCashService.find(model.getId());
		accountCashService.doCancleCash(cash, getOperator());
		printResult("取消提现操作成功！", true);
	}
	/**
	 * 设置提现成功
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/verifyCash")
	public void verifyCash() throws Exception {
		AccountCash cash = accountCashService.find(model.getId());
		accountCashService.verifyCash(cash, getOperator());
		printResult("确认提现成功操作成功！", true);
	}
	/**
	 *  重新触发本地提现队列
	 * @throws Exception
	 */
	@Action("/modules/account/cash/accountcash/reNotify")
	public void reNotify() throws Exception {
		AccountCash accountCash = accountCashService.find(model.getId());
		AccountCashModelValueEvent event = new AccountCashModelValueEvent();
		event.setOrderNo(accountCash.getOrderNo());
		QueueHandlers.submit(event);
		printResult("重新触发操作成功！", true);
	}

}
